package com.jfinal.aceadmin.intercepter;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aceadmin.util.StringUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;

public class LoginInterceptor implements Interceptor {

	@Override
	public void intercept(ActionInvocation ai) {
		Controller controller = ai.getController();
		String url = controller.getRequest().getRequestURI();
		HttpSession session = controller.getSession();
		String name = (String) session.getAttribute("name");
		String filterUrls = PropKit.use("server_config.properties").get(
				"filterUrls");
		List<String> urlList = StringUtil.spiltString2List(filterUrls);
		boolean result = false;
		if (!urlList.isEmpty()) {
			for (String fUrl : urlList) {
				if (url.contains(fUrl)) {
					result = true;
				}
			}
		}

		if (result) {
			ai.invoke();
		} else {
			if (StringUtils.isBlank(name)) {
				controller.redirect("/admin/user/login");
			} else {
				ai.invoke();
			}
		}
	}

}
